<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="审批人" prop="Approver">
          <a-select v-model="entity.Approver" allowClear placeholder="请选审批人">
            <a-select-option v-for="item in DepartmentHead" :key="item.UserId">{{ item.UserName }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="审批意见" prop="ApprovalOpinion">
          <a-input v-model="entity.ApprovalOpinion" autocomplete="off" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import store from '@/store'

export default {
  props: {
    parentObj: Object,
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 },
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {
        Number: [{ required: true, message: '必填' }],
      },
      title: '',
      rules: {
        Approver: [{ required: true, message: '必填' }],
        ApprovalOpinion: [{ required: true, message: '必填' }],
      },
      DepartmentHead: [],
    }
  },
  methods: {
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    getApproverList(id, requestId, state) {
      this.$http
        .post('/GBT_Manage/GBT_ServiceRequest/GetApprovalList', { departmentIds: id, requestId: requestId, state: state })
        .then((resJson) => {
          if (resJson.Success) {
            console.log('审批返回值：', resJson)
            this.DepartmentHead = resJson.Data
          } else {
            this.$message.error(resJson.Msg)
          }
        })
    },
    openForm(item, title) {
      this.init()
      if (item) {
        console.log('审批参数：', item)
        item.Approver = ''
        this.entity = item
        let deptId = ''
        switch (item.State) {
          case 0:
            deptId = item.RequestDepartmentId
            break
          case 1:
            deptId = item.DeliveryDepartmentId
            break
          case 2:
            deptId = item.RequestDepartmentId
        }
        console.log('部门信息：', JSON.parse(deptId))
        this.getApproverList(JSON.parse(deptId), item.Id, item.State)
      }
    },
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        this.visible = false
        console.log(this.entity)
        this.parentObj.approal(this.entity)
      })
    },
  },
}
</script>
